/*** 
 * @Author: Colin Dai
 * @Date: 2022-05-10 13:22:04
 * @LastEditors: Colin Dai
 * @LastEditTime: 2022-05-10 13:31:55
 * @FilePath: /jy-uos/src/softbus/discovery/discovery_service/source/bus_manager.c
 * @Description: 总线管理器
 * dailianzheng@163.com
 * Copyright (c) 2022 by Colin.Dai, All Rights Reserved. 
 */
#include "bus_manager.h"
#include "discovery_error.h"
#include "discovery_service_inner.h"
#include "log.h"


static int g_busStartFlag = 0;



//函数前置声明
int StartBus(void);
int StopBus(void);

/**
 * @brief 启动总线
 * @param {*}
 * @return {*}
 */
int StartBus(void)
{
    if (g_busStartFlag == 1) 
    {
        return 0;
    }
    
    CommonDeviceInfo *info = GetCommonDeviceInfo();
    if (info == NULL) {
        return ERROR_FAIL;
    }    


    /*
    int authPort = StartListener(&g_baseLister, info->deviceIp);
    if (authPort < 0) {
        SOFTBUS_PRINT("[AUTH] StartBus StartListener fail\n");
        return ERROR_FAIL;
    }
    */
    //info->devicePort = 0;

    /*

    int sessionPort = StartSession(info->deviceIp);
    if (sessionPort < 0) {
        SOFTBUS_PRINT("[AUTH] StartBus StartSession fail\n");
        StopListener();
        return ERROR_FAIL;
    }
    */
  

    g_busStartFlag = 1;

    SOFTBUS_PRINT("[BUS] StartBus ok\n");
    return 0;
}

/**
 * @brief 停止总线
 * @param {*}
 * @return {*}
 */
int StopBus(void)
{
    if (g_busStartFlag == 0) {
        return 0;
    }

    /*
    //FIXME 临时屏蔽
    //AuthMngDeInit();
    StopListener();

    int ret = RemoveTcpSessionMgr();
    if (ret != 0) {
        return ERROR_FAIL;
    }

    DeviceInfo *info = GetCommonDeviceInfo();
    if (info == NULL) {
        return ERROR_FAIL;
    }
    info->devicePort = -1;
    */

    g_busStartFlag = 0;
    return ERROR_SUCCESS;
}

int BusManager(unsigned int startFlag)
{
    if (startFlag == 1) {
        return StartBus();
    } else {
        return StopBus();
    }
}